import '../../index.dart';

class SelectData {
  late String id;
  late String name;
  late bool select;

  SelectData(this.id, this.name, {this.select = false});

  setupSelect(String? ids) {
    if (ids != null) {
      for (String id in ids.split(",")) {
        if (id == this.id) {
          select = true;
          return;
        }
      }
    }
    select = false;
  }

  static batchSetupSelect(List<SelectData> listData, String? ids) {
    for (SelectData selectData in listData) {
      selectData.setupSelect(ids);
    }
  }
}

class SelectPage extends StatefulWidget {
  /*标题*/
  late String title;

  /*是否单选*/
  late bool isRadio;

  /*待选择数据*/
  late List<SelectData> listData;

  /*选中ids ,分割*/
  late String? selectIds;

  SelectPage(
      {Key? key,
      this.isRadio = true,
      required this.listData,
      this.selectIds = "",
      this.title = "请选择"})
      : super(key: key);

  @override
  _SelectState createState() => _SelectState();
}

class _SelectState extends State<SelectPage> {
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    SelectData.batchSetupSelect(widget.listData, widget.selectIds);
  }

  @override
  Widget build(BuildContext context) {
    return GlobalStyle(
      child: BackgroundStyleWidget(
        child: Scaffold(
          appBar: AppBar(
            actions: widget.isRadio
                ? null
                : [
                    TextButton(
                        onPressed: () {
                          String ids = "";
                          String names = "";
                          for (SelectData selectData in widget.listData) {
                            if (selectData.select) {
                              if (ids.isNotEmpty) {
                                ids += ",";
                              }
                              if (names.isNotEmpty) {
                                names += ",";
                              }
                              ids += selectData.id;
                              names += selectData.name;
                            }
                          }
                          if (ids.isNotEmpty) {
                            Navigator.pop(context, [ids, names]);
                          } else {
                            showToast("未选择内容");
                          }
                        },
                        child: Text("确定"))
                  ],
            title: Text(
              widget.title,
            ),
            centerTitle: true,
          ),
          body: _buildBody(), // 构建主页面
        ),
      ),
    );
  }

  Widget _buildBody() {
    return ListView.separated(
      itemCount: widget.listData.length,
      itemBuilder: (context, index) {
        return ListTile(
          title: Text(widget.listData[index].name),
          trailing: widget.listData[index].select ? Icon(Icons.check,color: Theme.of(context).primaryColor,) : null,
          onTap: () {
            if (widget.isRadio) {
              Navigator.pop(context,
                  [widget.listData[index].id, widget.listData[index].name]);
            } else {
              //修改选择状态
              setState(() {
                widget.listData[index].select = !widget.listData[index].select;
              });
            }
          },
        );
      },
      separatorBuilder: (BuildContext context, int index) {
        return const Divider(height: 1,);
      },
    );
  }
}
